﻿namespace LocationByAddress
{
    using System;
    using System.Reactive.Linq;
    using System.Windows;
    using Microsoft.Phone.Controls;
    using WP7Contrib.Services.BingMaps;
    using WP7Contrib.Services.BingMaps.Model;

    public partial class MainPage : PhoneApplicationPage
    {
        private readonly IBingMapsService bingMapsService;

        public MainPage()
        {
            InitializeComponent();

            bingMapsService = new BingMapsService("Your credentials id", "Your app id");

            this.postCodeText.Text = "94700";
            this.countryRegionText.Text = "France";
        }

        private void locationByPostCode_Click_1(object sender, RoutedEventArgs e)
        {
            var criterion = CriterionFactory.CreateLocationSearchForAddress(this.postCodeText.Text);
            bingMapsService.SearchForLocationUsingAddress(criterion)
                .ObserveOnDispatcher()
                .Subscribe(this.DisplayAddresses, this.FailedAddress);
        }
        
        private void locationByPostCodeAndCountry_Click(object sender, RoutedEventArgs e)
        {
            var country = this.countryRegionText.Text;
            var postCode = this.postCodeText.Text;

            var criterion = CriterionFactory.CreateLocationSearchForAddress(country, postCode);
            bingMapsService.SearchForLocationUsingAddress(criterion)
                .ObserveOnDispatcher()
                .Subscribe(this.DisplayAddresses, this.FailedAddress);
        }

        private void locationByFullCountry_Click(object sender, RoutedEventArgs e)
        {
            var searchAddress = new Address
                                    {
                                        AddressLine = this.addressLineText.Text,
                                        AdminDistrict = this.adminDistrictText.Text,
                                        CountryRegion = this.countryRegionText.Text,
                                        PostalCode = this.postCodeText.Text
                                    };
            var criterion = CriterionFactory.CreateLocationSearchForAddress(searchAddress);
            bingMapsService.SearchForLocationUsingAddress(criterion)
                .ObserveOnDispatcher()
                .Subscribe(this.DisplayAddresses, this.FailedAddress);
        }

        private void FailedAddress(Exception exception)
        {
            this.resultStatus.Text = "Failed to search for address!";
            this.address.Text = string.Format("Exception - '{0}'", exception.Message);
        }

        private void DisplayAddresses(LocationSearchResult result)
        {
            this.resultStatus.Text = string.Format("Response: {0} Locations", result.Locations.Count);
            this.address.Text = string.Empty;

            if (result.HasLocations)
            {
                foreach (var location in result.Locations)
                {
                    this.address.Text += string.Format("{0}, ({1} / {2})", 
                                                        location.Address.FormattedAddress,
                                                        Math.Round(location.Point.Latitude, 5),
                                                        Math.Round(location.Point.Longitude, 5));

                    this.address.Text += Environment.NewLine;
                }
            }
            else
            {
                this.address.Text = string.Empty;
                this.resultStatus.Text = "Response: No Locations!";
            }
        }
    }
}